from django.db import models
from django.contrib.auth.models import User

from managers import TimeManager
from managers import ProjectManager
from managers import DateManager
from managers import ActivityProjectManager

from manager.model_manager.time import TimeConfirmManager
from manager.model_manager.project import ProjectDataManager


class UserGroup(models.Model):
    is_manager = models.BooleanField()
    is_financier = models.BooleanField()
    is_hr = models.BooleanField()
    user = models.OneToOneField(User)

    def __unicode__(self):
        return u'%s %s %s %s %s' % (self.user ,self.is_developer,
                        self.is_manager, self.is_financier, self.is_hr)


class Activity(models.Model):
    name_activity = models.CharField(max_length=30)
    
    def __unicode__(self):
        return self.name_activity


class Project(models.Model):
    name_project = models.CharField(max_length=100)

    objects = ProjectManager()
    take_project = ProjectDataManager()
    
    def __unicode__(self):
	    return self.name_project

    
class Date(models.Model):
    day = models.DateField()

    objects = DateManager()
    
    @staticmethod
    def find_index_date(date_list, day):
        i = 0
        while (i <= (len(date_list) - 1)):
            if (day == date_list[i]):
                break;
            else:
                i += 1
        return i

    @staticmethod
    def take_date(day):
        try:
            day = Date.objects.get(day=day)
        except(Date.DoesNotExist):
            day = Date.objects.create(day=day)
        return day

    def __unicode__(self):
        return self.day


class ActivityProject(models.Model):
    user = models.ForeignKey(User)
    
    activity = models.ForeignKey(Activity)
    project = models.ForeignKey(Project)

    objects = ActivityProjectManager()
    
    @staticmethod
    def take_create_activityproject(user, id_user, post, projects, activities):
        try:
            activity_project = ActivityProject.objects.take_activityproject(
                                                   id_user, post['projects'],
                                                           post['activities'])
        except(IndexError):
            activity_project = ActivityProject.objects.create(user=user,
                         project=projects,
                         activity=activities)
        return activity_project
    
    def __unicode__(self):
        return (self.user.username + ' ' +self.activity.name_activity + ' ' 
                 + self.project.name_project)


class Time(models.Model):
    time = models.FloatField(blank=True, null=True)
    details = models.TextField()
    
    activity_project = models.ForeignKey(ActivityProject)
    day = models.ForeignKey(Date)
    is_confirmed = models.BooleanField(default=0)
    is_completed = models.BooleanField()

    objects = TimeManager()
    take_time = TimeConfirmManager()

    @staticmethod
    def update_time_from_post(times, post):
        for time in times:
            name_time = 'time' + ' ' + str(time[4])
            name_details = 'details' + ' ' + str(time[4])
            try:
                value_time = float(post[name_time])
            except(ValueError):
                continue
            value_details = post[name_details]
            if value_time == '': value_time = 0
            time = Time.objects.update_time(time[4], value_time, value_details)
        return

    @staticmethod
    def save_time_in_completion(id_user, data):
        for i in data:
            activity_project = ActivityProject.objects.take_exist_activityproject(
                                                                id_user, i[0], i[1])
            try:
                day = Date.objects.get(day=i[2])
            except(Date.DoesNotExist):
                day = Date.objects.create(day=i[2])
            try:
                time = Time.objects.get(activity_project=activity_project,
                                        day=day)
                time.time = i[3]
                time.save()
            except(Time.DoesNotExist):
                Time.objects.create(activity_project=activity_project,
                                    time=i[3],
                                   details='', day=day)
        return

    def __float__(self):
        return self.time
